<?php
/**
 * Author: Jesse
 * Email : jessedev@163.com
 * Date: 2024/6/14 23:53
 * FileName: Response.php
 * DESC: The Relentless Pursuit of Perfection.
 */

namespace Common\Kernel\Http;

use Common\Constants\ResponseCode;
use Hyperf\HttpServer\Response as HyperfResponse;
use Psr\Http\Message\ResponseInterface;

/**
 * 响应类
 */
class Response extends HyperfResponse
{
    /**
     * @param int $code
     * @param mixed $data
     * @param string $message
     * @return array
     */
    protected function formatJson(int $code, mixed $data, string $message): array
    {
        return [
            'code' => $code,
            'data' => $data,
            'message' => $message,
        ];
    }

    /**
     * 处理成功信息返回
     *
     * @param mixed $data
     * @param int $code
     * @param ?string $message
     * @return ResponseInterface
     */
    public function successJson(mixed $data = [], int $code = ResponseCode::SUCCESS, string $message = null): ResponseInterface
    {
        return $this->json($this->formatJson($code, $data, $message ?? ResponseCode::getMessage($code)));
    }

    /**
     * 处理失败信息返回.
     *
     * @param int $code
     * @param array $data
     * @param string|null $message
     * @return ResponseInterface
     */
    public function errorJson(int $code = ResponseCode::ERROR, string $message = null, mixed $data = []): ResponseInterface
    {
        return $this->json($this->formatJson($code, $data, $message ?? ResponseCode::getMessage($code)));
    }
}